package com.hp.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hp.entity.Announcement;
import com.hp.service.AnnouncementService;
import com.hp.util.JsonData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/ann")
public class AnnouncementController {

    @Resource
    private AnnouncementService announcementService;

    @RequestMapping("/list")
    @ResponseBody
    public Object list(Announcement announcement, @RequestParam(value = "page",defaultValue = "1")Integer page,
                       @RequestParam(value = "limit",defaultValue = "10")Integer limit) {
        PageHelper.startPage(page,limit);
        List<Announcement> list = announcementService.list(announcement);
        return JsonData.buildSuccess(new PageInfo<>(list));
    }
    //删除
    @RequestMapping("/del")
    @ResponseBody
    public Object del(Integer id) {
        int i = announcementService.del(id);
        return JsonData.buildSuccess(i>0?"删除成功！":"删除失败！");
    }

    //新增
    @RequestMapping("/add")
    @ResponseBody
    public Object add(Announcement announcement) {
        int i= announcementService.add(announcement);
        return JsonData.buildSuccess(i>0?"新增成功！":"新增失败！");
    }

    //通过findById查询
    @RequestMapping("/findById")
    @ResponseBody
    public Object findById(Integer id) {
        Announcement announcement = announcementService.findById(id);
        System.out.println(announcement);
        return JsonData.buildSuccess(announcement);
    }
    //修改
    @RequestMapping("/update")
    @ResponseBody
    public Object update(Announcement announcement) {
        int i = announcementService.update(announcement);
        System.out.println(i);
        return JsonData.buildSuccess(i>0?"修改成功！":"修改失败！");
    }
}